/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Bárbara
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class PostgreSQL {
    private static Connection con;
	private String driver, banco, login, senha;
	
	public PostgreSQL(){
	    this.driver = "org.postgresql.Driver";
	    this.banco = "jdbc:postgresql://localhost/pesquisaEleitoral";
	    this.login = "postgres";
	    this.senha = "123456";
	}
	
	public Connection getConexao() throws BancoException{
		try{
			if(PostgreSQL.con == null){
				Class.forName(this.driver);
				PostgreSQL.con = DriverManager.getConnection(this.banco, this.login, this.senha);
			}
			return PostgreSQL.con;			
		}catch (ClassNotFoundException cnfe) {
			throw new BancoException("[PostgreSQL] ClassNotFoundException: " + cnfe.getMessage());
		} catch (SQLException sqle) {
			throw new BancoException("[PostgreSQL] BancoException: " + sqle.getMessage());
		}
	}
	
	public void close() throws BancoException{
		try {
		    PostgreSQL.con.close();
		} catch (SQLException sqle) {
			throw new BancoException("[PostgreSQL] BancoException: " + sqle.getMessage());
		}
	}
}
